<?php

namespace app\common\model;

use basic\ModelBasic;

class GoldsGoods extends ModelBasic
{
	use \traits\ModelTrait;

    /** 商品类型 */
    const GOOD_TYPE_COUPON = 6;
    public static $goods_type = [
        0 => '全部',
        1 => '厨房用具',
        2 => '电子数码',
        3 => '品质生活',
        4 => '爱车达人',
        5 => '日用百货',
        6 => '代金券',
    ];

	/** 购买方式 **/
    const PAY_GOLDS     = 1;
    const PAY_GOLDS_RMB = 2;
    const PAY_RMB       = 3;
	public static $pay_type = [
	    1 => '纯金币',
	    2 => '金币+人民币',
	    3 => '人民币',
	];

	 /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field("m.*,m.goods_name as title")
        ->page($where['page'] ?? 1,$where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * [getOne description]
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
            ->field("m.*")
            ->find();

        if ( empty($item) ) {
            return [];
        }

        $item = $item->toArray();

        $item['details'] = CommonDetail::getDetails('mall',$item['id'],false);
        return static::process_list([$item])[0];
    }

     /**
     * 处理列表数据
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ($list as $key => &$value) {
            $value['goods_type_text'] = static::$goods_type[$value['goods_type']] ?? '-';;
            $value['pay_type_text']   = static::$pay_type[$value['pay_type']] ?? '-';;
            $value['status_text']     = static::$common_status[$value['status']] ?? '-';
            $value['albums']          = json_decode($value['albums']);
            $value['rels']            = ['rel_id' => 0];
            if ( $value['goods_type'] == self::GOOD_TYPE_COUPON ) {
                $value['rels'] = GoodsRels::where('goods_id',$value['id'])->where('rel_type',self::GOOD_TYPE_COUPON)->find();
            }
        }
        return $list;
    }

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['name']) && $where['name'] != '' ) {
            $model->where( sprintf('%s.name' , $alias) . ' LIKE ' . "'%" . $where['name'] ."%'");
        }

        if ( isset($where['status']) && $where['status'] != '' ) {
            $model->where(sprintf('%s.status = %d' , $alias , $where['status']));
        }

        if ( isset($where['goods_type']) && $where['goods_type'] > 0 ) {
            $model->where(sprintf('%s.goods_type = %d' , $alias , $where['goods_type']));
        }

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order(sprintf('%s.%s' , $alias, $where['order']));
        } else {
            $model->order(sprintf('%s.id ASC' , $alias));
        }

        return $model;
    }

     /**
     * 主数据保存后回调
     * @Author   FW
     * @DateTime 2021-08-10
     * @Email    123763712@qq.com
     * @param    [type]           $prk_id [description]
     * @param    array            $data   [description]
     * @return   [type]                   [description]
     */
    public static function afterSave ( $prk_id , array $data )
    {
         ## 详情
        if ( isset($data['details']) ) {
            $res = CommonDetail::addDetails('mall',$prk_id,$data['details']);
            if ( $res === false ) throw new \Exception('保存详情失败');
        }

        if ( isset($data['rels']) ) {
            if ( isset($data['rels']['rel_id']) && ($data['rels']['rel_id'] > 0) ) {
                $res = GoodsRels::saveOne([
                    'goods_id' => $prk_id,
                    'rel_type' => $data['goods_type'],
                    'rel_id'   => $data['rels']['rel_id']
                ]);
                if ( $res === false ) throw new \Exception('保存失败[10823]');
            }
        }

        return true;
    }
}//Class End
